package com.efounder.frame.utils;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.efounder.mobilemanager.R;
import com.efounder.util.AppContext;
import com.efounder.util.ToastUtil;
import com.efounder.utils.ResStringUtil;
import com.utilcode.util.NetworkUtils;


/**
 * 获取网络状态的工具类
 * Created by XinQing on 2016/12/2.
 */

public class NetStateBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "NetStateBroadcastReceiver";

    /**
     * 无网络
     **/
    public static final int NET_UNREACHABLE = 0;
    /**
     * wifi
     **/
    public static final int NET_WIFI = 1;
    /**
     * 流量
     **/
    public static final int NET_MOBILE = 2;
    /**
     * 当前网络状态
     **/
    private int net_state;

    /**
     * 获取当前网络类型
     **/
    private static int getNetType() {

        int netType = NET_UNREACHABLE;
        ConnectivityManager cm = (ConnectivityManager) AppContext.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo == null) {
            return netType;
        }
        int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_MOBILE) {
            netType = NET_MOBILE;
        } else if (nType == ConnectivityManager.TYPE_WIFI) {
            netType = NET_WIFI;
        }
        return netType;
    }

    private NetStateListener netStateListener;


    public NetStateBroadcastReceiver() {
        super();
        net_state = getNetType();

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            ConnectivityManager connectivityManager = (ConnectivityManager) AppContext.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isAvailable()) {
                String name = networkInfo.getTypeName();
                if (net_state == NET_UNREACHABLE) {
                    ToastUtil.showToast(AppContext.getInstance(), ResStringUtil.getString(R.string.common_text_net_restored));
                }
                if ("wifi".equals(name.toLowerCase())) {
                    net_state = NET_WIFI;
                } else {
                    net_state = NET_MOBILE;
                }
                //连上网络，尝试登录
                if (netStateListener != null) {
                    netStateListener.onConnected(net_state);
                }

            } else {
                ToastUtil.showToast(AppContext.getInstance(), ResStringUtil.getString(R.string.common_text_net_not_available));
                net_state = NET_UNREACHABLE;
                //监听网络状态的观察者派发
                if (netStateListener != null) {
                    netStateListener.onDisconnected(net_state);
                }

            }


        }
    }

//    /**
//     * 获取网络状态
//     *
//     * @return
//     */
//    public static int getNetState() {
//        return net_state;
//    }

    /**
     * 判断是否有网络
     *
     * @return
     */
    public static boolean isNetActive() {
        return NetworkUtils.isConnected();
//        return net_state != NET_UNREACHABLE;
    }

    public NetStateListener getNetStateListener() {
        return netStateListener;
    }

    public void setNetStateListener(NetStateListener netStateListener) {
        this.netStateListener = netStateListener;
    }

    /**
     * 对网络状况进行监听
     */
    public interface NetStateListener {
        void onConnected(int netState);

        void onDisconnected(int netState);
    }
}

